package com.controller;


import com.entity.BUser;
import com.entity.table.BUserTableDef;
import com.mybatisflex.core.query.QueryWrapper;
import com.service.BUserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private BUserService bUserService;

    @GetMapping("/login")
    public String toLoginPage(){
        return "back/login";
    }

    @PostMapping("/toLogin")
    public String login(BUser bUser, HttpSession httpSession, Model model){
//        BUser bUser1 = bUserService.userLogin(bUser);
        QueryWrapper query = QueryWrapper.create()
                .where(BUserTableDef.B_USER.USERNAME.eq(bUser.getUsername()))
                .and(BUserTableDef.B_USER.PASSWORD.eq(bUser.getPassword()));
        BUser bUser1 = bUserService.getOne(query);
        if (bUser1!= null){
            httpSession.setAttribute("admin",bUser);
            model.addAttribute("admin",bUser1);
            return "back/index";
        }
        else {
            model.addAttribute("msg","账号或密码错误，请重新输入！");
            return  "back/login";
        }
    }

    @GetMapping(value = "ue")
    public String ue(Model model){
        return "index";
    }
}
